package test

import bankofficegrepper.BankInfoWriter
import bankofficegrepper.CMBBankInfoResolver
import com.jleo.BankInfoBuffer
import module.CMBMainPageModule
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.telluriumsource.test.java.TelluriumJUnitTestCase

class CMBTestCase extends TelluriumJUnitTestCase {
  private static CMBMainPageModule cmbPage
  private static BankInfoBuffer buffer

  @BeforeClass
  public static void initUi() {
    buffer = new BankInfoBuffer()
    cmbPage = new CMBMainPageModule(buffer)
    cmbPage.defineUi();
    cmbPage.disableCssSelector()
  }

  @Before
  public void connectToPage() {
    connectSeleniumServer()

    connectUrl 'http://www.cmbchina.com/CmbWebPubInfo/cmbbranch/subbranch_list.aspx'
  }

  //Add your test cases here

  @Test
  public void testMain() {
    // def cities = cmbPage.getAllCities()
    // cities.each {
    def allBankInfo = []
    Thread comsumer = new Thread(new Runnable() {
      void run() {
        while (!buffer.produceFinish || buffer.size > 0) {
          allBankInfo.addAll new CMBBankInfoResolver().resolveData(buffer.consume())
        }
        new BankInfoWriter("cmb.txt").writeTo(allBankInfo)
      }
    }).start()

    cmbPage.getAllCities().each {
      cmbPage.selectCity(it)
    }
    buffer.produceFinish = true


  }
}
